package badgegenerator.controller;

import badgegenerator.dao.UserDAO;
import badgegenerator.dao.exceptions.DAOException;
import badgegenerator.dao.exceptions.MyBatisInitException;
import badgegenerator.model.User;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;

@ManagedBean
@RequestScoped
public class LoginController {
    @ManagedProperty(value="#{userMainPageController}")
    private UserMainPageController mainPageController;
                
    private String formUserName;
    private String formUserPassword;

    public UserMainPageController getMainPageController() {
        return mainPageController;
    }

    public String getFormUserName() {
        return formUserName;
    }

    public String getFormUserPassword() {
        return formUserPassword;
    }

    public void setMainPageController(UserMainPageController mainPageController) {
        this.mainPageController = mainPageController;
    }

    public void setFormUserName(String formUserName) {
        this.formUserName = formUserName;
    }

    public void setFormUserPassword(String formUserPassword) {
        this.formUserPassword = formUserPassword;
    }

    public LoginController() {
        
    }   
    
    public String moveToMainPage() throws DAOException, MyBatisInitException{
        UserDAO userDAO = new UserDAO();
        User curUser = userDAO.getUserByNickName(formUserName);
        if(curUser != null && formUserPassword.equals(curUser.getPassword())){
            mainPageController.setUser(curUser);
            return "userMainPage";
        }
        
        return "errorPage";
    }
}
